package com.oa.login.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.script.ScriptException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oa.jixiao.utils.ImgCodeUtil;
import com.oa.login.biz.EmpBiz;
import com.oa.login.biz.UsersBiz;
import com.oa.login.entity.Emp;
import com.oa.login.entity.Users;
import com.oa.login.entity.UsersVO;
import com.oa.xinxi.utils.MD5Utils;


@RequestMapping("/check")
@Controller
public class UserController {
	@Resource
	private UsersBiz userBiz;
	
	@Resource
	private EmpBiz empBiz;
	
	@ResponseBody
	@RequestMapping("/login")
    public String doLogin(String username, String password,HttpServletRequest request,String yzms){
		String imgCode = (String)request.getSession().getAttribute("imgCode");
		if(!imgCode.equalsIgnoreCase(yzms)){
			return "error";
		}else{
			 userBiz.login(username, password);
	         Subject user=SecurityUtils.getSubject();
	         Users u=new Users();
	         u.setUsername(username);
	         boolean flag=user.isAuthenticated();
	         if(flag){
	        	 int id=userBiz.find(u);
	        	 Emp emp=empBiz.findById(id);
	        	 request.getSession().setAttribute("emp", emp);
	        	 request.getSession().setAttribute("number", username);
	        	 request.getSession().setAttribute("uname", emp.getEmpName());
	        	 return "sucess";
	         }else{
	        	 return "fail";
	         }
		}
       
         
//        System.out.println(user.getPrincipal());
//        System.out.println(user.isAuthenticated());
//        System.out.println(user.hasRole("admin"));
//        System.out.println(user.isPermitted("sys:system:update"));
    }
	
	@RequestMapping("/index")
	public String index(){
		return "/index";
	}
	
	
	@RequestMapping("/createImg.do")
	  protected void createImg(HttpServletRequest req, HttpServletResponse res)
			    throws IOException, ScriptException
			  {
			    Object[] objs = ImgCodeUtil.createImage();

			    String imgCode = (String)objs[0];
			    HttpSession session = req.getSession();
			    session.setAttribute("imgCode", imgCode);

			    res.setContentType("image/jpeg");

			    BufferedImage img = (BufferedImage)objs[1];

			    OutputStream os = res.getOutputStream();
			    ImageIO.write(img, "jpeg", os);
			    os.close();
			  }

	@ResponseBody
	@RequestMapping("/daka")
    public String daka(Users users){
			Users u=userBiz.findByUser(users);
	         if(u!=null){
	        	 return "sucess";
	         }else{
	        	 return "fail";
	         }
		
	}
	
	
	@RequestMapping("/list")
	public String list(Model model){
		List<Emp> empList =empBiz.findAlls();
		List<UsersVO> list=new ArrayList<>();
		for(Emp e:empList){
			UsersVO u =new UsersVO();
			u.setUserId(e.getUserId());
			u.setUsername(e.getEmpName());
			u.setTel(e.getEmpTel());
			u.setNumber(userBiz.findById(e.getUserId()));
			list.add(u);
		}
		model.addAttribute("list", list);
		return "/jixiao/list";
	}
	
	@RequestMapping("/add")
	public String add(){
		return "jixiao/add";
	}
	
	@RequestMapping("doAdd")
	public String doAdd(Emp e,Users u){
		u.setPassword(MD5Utils.GetMD5Code(u.getPassword()));
		userBiz.save(u);
		e.setUserId(u.getId());
		empBiz.save(e);
		return "redirect:/check/list";
	}
	
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable("id") int id){
		empBiz.del(id);
		userBiz.del(id);
		return "redirect:/check/list";
	}
}
